package bavaria.hightech.banking.statement;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Iterator;

import bavaria.hightech.banking.accounts.Account;
import bavaria.hightech.banking.lists.Booking;
import bavaria.hightech.banking.resourceBundles.LanguageBundle;

/**
 * class KtoAuszugsFormiererHtmlImpl implements KtoAuszugsFormartierer
 * @author David
 *
 */
public class KtoAuszugsFormatiererHtmlImpl implements KtoAuszugsFormatierer {

	Iterator<Booking> iterator;
	LanguageBundle languageBundle;

	@Override
	public void specifyAccount(Account acc) {
		iterator = acc.getBookingsListIterator();
		languageBundle = acc.languageBundle;
	}

	@Override
	public String getAuszug() {
		Booking current;
		String auszug = new String();
		while (iterator.hasNext()) {
			current = iterator.next();
			auszug += formatOneField(current.getBookingTime(),
					current.getAmount(), current.getReason());
		}
		return auszug;
	}
	
	/**
	 * formate one field
	 * @param bookingTime
	 * @param amount
	 * @param reason
	 * @return
	 */
	private String formatOneField(Date bookingTime, long amount, String reason) {
		String html = new String();
		String css = new String();

		String formattedAmount = NumberFormat.getNumberInstance(
				languageBundle.getLocale()).format(amount);
		String formattedDate = DateFormat.getDateInstance(DateFormat.MEDIUM,
				languageBundle.getLocale()).format(bookingTime);

		css += "<style type=\"text/css\">" + "\n";
		css += "  .left { float:left; margin-right:10px; }" + "\n";
		css += "  .clear { clear:left; height:15px; }" + "\n";
		css += "</style>" + "\n";

		html += "<div class=\"left\">{19}: <br>" + formattedDate + "</div>"
				+ "\n";
		html += "<div class=\"left\">{0}: <br>"
				+ ((amount <= 0) ? "{14}" : "{15}") + "</div>" + "\n";
		html += "<div class=\"left\">{16}: <br>" + formattedAmount + "</div>"
				+ "\n";
		html += "<div class=\"left\">{17}: <br>" + reason + "</div>" + "\n";
		html += "<div class=\"clear\"></div>" + "\n";

		html = MessageFormat.format(html, languageBundle.getContents());

		return css + html;
	}

}
